/*
 * Copyright (c) Scrap Material Junior Brother, Xin. 2023-2023. All rights reserved.
 */

package collection.sort;

import java.util.Objects;

/**
 * 重写了Comparable的人
 *
 * @author xin
 * @since 2023-05-02
 */
public class ComparablePerson implements Comparable<ComparablePerson> {
    private String name;
    private Integer age;
    ComparablePerson(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "ComparablePerson{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ComparablePerson)) {
            return false;
        }
        ComparablePerson that = (ComparablePerson) o;
        return Objects.equals(name, that.name) &&
                Objects.equals(age, that.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(ComparablePerson o) {
        int data = age.compareTo(o.age);
        return data == 0 ? name.compareTo(o.name) : data;
    }
}
